class Solution {
public:
    int happysum(int n)
    {
        int sum=0;
        while(n)
        {
            int tmp=n%10;
            sum+=(tmp*tmp);
            n/=10;
        }
        return sum;
    }
    bool isHappy(int n) {
        int slow=n;
        int first=happysum(n);
        while(slow!=first)
        {
            slow=happysum(slow);
            first=happysum(first);
            first=happysum(first);
        }
        return slow==1;
    }
};
